package com.example.QQ;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import serverside.UserDAO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.QQ.SendMessage;



public class Controller{

    @FXML
    public TextField NumberTextField;
    @FXML
    private TextField PasswordTextField;
    @FXML
    private TextField IDText;
    @FXML
    private TextField NameText;
    @FXML
    private TextField AddText;
    
    
    private String pic;
    private FileInputStream fileInputStream;
    @FXML
    private TextField PasswordText;
    @FXML
    private Button uploadpic;
    @FXML
    private TextArea SendMessage;
    @FXML
    private TextArea text;
    @FXML
    private TextArea Message;
    @FXML
    private Button Login;
    @FXML
    private Button ApplyExit;
    @FXML
    private Button confirmadd;
    @FXML
    private Button SendMessageButton;
    @FXML
    private Button addfriends;
    @FXML
    private Label State;
    @FXML
    private Button Chat;
    @FXML
    private Button Apply;
    @FXML
    private Label welcomeText;
    @FXML
    private Label loginMessageLabel;
    @FXML
    private Label AddMessageLabel;
    @FXML
    private Label AttentionMessage;
    @FXML
    protected void onHelloButtonClick() {
        welcomeText.setText("QQ聊天软件");
    }
    
    
    UserDAO userdao =new UserDAO();
    String id;
    //登录界面
   @FXML
   public void Login() throws IOException, SQLException {
	   
	   if(NumberTextField.getText().isBlank() == false && PasswordTextField.getText().isBlank() == false){

		   System.out.print(id);
           if(userdao.validateLogin(NumberTextField.getText(),PasswordTextField.getText())==1) {
        	   NumberTextField.setUserData(NumberTextField.getText());
        	   loginMessageLabel.setText("login scuccessfully!");
				  Logining login = new Logining();//登录中窗口
		           login.Logining();//打开新窗口         
		                Stage primaryStage = (Stage) Login.getScene().getWindow();
		                
		              primaryStage.close();//打开新的窗口 关闭当前的窗口
		              MainApplication mainApplication = new MainApplication();//新窗口类首页窗口
		               mainApplication.MainApp();//打开新窗口
		               List<String> person=new ArrayList<>();
		               for(int i = 0;i <userdao.statistics("111") ;i++) {
		            	   Label id =new Label(userdao.huahua(i));
		            	   person.add(userdao.huahua(i));
		            	   GridPane gr = new GridPane();
		            	   gr.add(id, i*10, i*10);
		            	   gr.setAlignment(Pos.CENTER);

		            	   
		               }
		               System.out.println(person);
		               
           }
           else {
        	   loginMessageLabel.setText("登录失败，重新搞");
           }
                       
  }
     else {
    	 loginMessageLabel.setText("重新输入啊,bro");
         LoginError input = new LoginError();
           input.InputNull();
  }
}
   

	//打开账号申请界面
    @FXML
    public void Apply(ActionEvent event) throws IOException {
    	
        ApplyApplication apply = new ApplyApplication();//新窗口类
        apply.Apply();//打开新窗口
        
    }

    //账号申请
    @FXML
    public void ApplyExit(ActionEvent event) throws IOException {
    
        Stage primaryStage = (Stage) ApplyExit.getScene().getWindow();
        if(IDText.getText().isEmpty() || NameText.getText().isEmpty() || PasswordText.getText().isEmpty()){
        	
            Attention input = new Attention();
            input.InputNull();
        }
        else {
        	FileInputStream fileInputStream = new FileInputStream(pic);
        	userdao.register(IDText.getText(),PasswordText.getText(),NameText.getText(),fileInputStream);
            primaryStage.close();
        }
      }
 //注册账号打开文件的方法   
    public void uploadpic(ActionEvent event) throws FileNotFoundException{
    	FileChooser fileChooser = new FileChooser();
    	FileChooser.ExtensionFilter extensionFilter =new FileChooser.ExtensionFilter("jpg文件", "*.jpg");
    	fileChooser.setSelectedExtensionFilter(extensionFilter);
    	
    	File result = fileChooser.showOpenDialog(new Stage());
    	pic = result.getAbsolutePath();

    	System.out.print(pic);
    }
    
    //加好友
    @FXML    
    public void confirmadd(ActionEvent event) throws IOException {

    	if(AddText.getText().isEmpty()||(userdao.judge(AddText.getText())==0)) {
    		AddMessageLabel.setText("没有这个bro");
    		
    	}else{
    		
    		userdao.add("111", AddText.getText());
    		  		
    	}
    	
    }
    //打开添加好友窗口
    @FXML    
    public void addfriends(ActionEvent event) throws IOException {
    	
    	AddFriends Add = new AddFriends();//新窗口类
        Add.AddFriend();//打开添加好友新窗口
    }
    //打开聊天界面
    @FXML
    public void Chat(ActionEvent event) throws IOException {

        ChatApplication Chat = new ChatApplication();//新窗口类
        Chat.Chat();//打开聊天新窗口

    }


    @FXML
    public void send(ActionEvent actionEvent) throws IOException {
        Date date = new Date();
        text.setEditable(false);
       new SendMessage(Message.getText()).start();
        //new SendMessage(Message.getText(),new Server().getServer().get(0)).start();
        text.setText(text.getText() + "\r\n" + date + "\r\n" + "您：  "+Message.getText());
        Message.setText("");
    }
    public void test(String message) throws IOException {
    	text.setEditable(false);
        Date date = new Date();
        text.setText(text.getText() + "\r\n" + date + "\r\n" + "好友："+message);
    }
   

   }
